# -*- encoding: utf-8 -*-

DAILY_USER_REPORT = {
    'real_time_report': True,
    'target_event_id': ['recharge', 'withdraw', 'active', 'register', 'bind_phone', 'cp_lottery', 'cp_game'],
    'values': [
        {
            'value_id': 'new_user',
            'target_value': '_user_id',
            'value_name': '新增用户',
            'filter': [
                {'terms': {'_event_id': ['register']}},
            ],
            'operation': 'cardinality',
        },
        {
            'value_id': 'new_bind_user',
            'target_value': '_user_id',
            'value_name': '当日新增绑定用户数',
            'filter': [
                {'term': {'_event_id': 'bind_phone'}},
                {'term': {'_is_new_user': 1}}
            ],
            'operation': 'cardinality',
        },
        {
            "value_id": "active_user",
            "target_value": "_user_id",
            "value_name": "今日登录",
            "filter": [
                {"term": {"_event_id": "active"}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "active_ios_user",
            "target_value": "_user_id",
            "value_name": "今日iOS登录",
            "filter": [
                {'term': {'_event_id': 'active'}},
                {'term': {'_chn.keyword': 'ios'}}
            ],
            "operation": "cardinality",
            "value_type": "long"
        },
        {
            "value_id": "active_android_user",
            "target_value": "_user_id",
            "value_name": "今日Android登录",
            "filter": [
                {"term": {"_event_id": "active"}},
            ],
            "not_filter": [
                {'term': {'_chn.keyword': 'ios'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_ip_register",
            "target_value": "_ip.keyword",
            "value_name": "首次登录的ip",
            "filter": [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality"
        },
        {
            'value_id': 'bind_user',
            'target_value': '_user_id',
            'value_name': '当日绑定用户数',
            'filter': [
                {'term': {'_event_id': 'bind_phone'}}
            ],
            'operation': 'cardinality',
        },

        {
            "value_id": "recharge_amount",
            "target_value": "recharge_price",
            "value_name": "当日总付费金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
        },
        {
            "value_id": "new_recharge_amount",
            "target_value": "recharge_price",
            "value_name": "当日新增付费金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
        },
        {
            "value_id": "agent_recharge_amount",
            "target_value": "recharge_price",
            "value_name": "当日代理付费金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'terms': {'recharge_recharge_type': ['wechat', 'mf_pay']}}
            ],
        },
        {
            "value_id": "new_agent_recharge_amount",
            "target_value": "recharge_price",
            "value_name": "当日新增代理付费金额",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'terms': {'recharge_recharge_type': ['wechat', 'mf_pay']}},
                {'term': {'_is_new_user': 1}}
            ],
        },
        {
            "value_id": "recharge_user",
            "target_value": "_user_id",
            "value_name": "付费账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "new_recharge_user",
            "target_value": "_user_id",
            "value_name": "新增付费账号",
            "filter": [
                {'term': {'_event_id': 'recharge'}},
                {'term': {'_is_new_user': 1}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "online_recharge_user",
            "value_name": "在线充值账号",
            "value_expr": "recharge_user - agent_recharge_user",
        },
        {
            "value_id": "online_new_recharge_user",
            "value_name": "新用户在线充值账号",
            "value_expr": "new_recharge_user - agent_new_recharge_user",
        },
        {
            "value_id": "agent_recharge_user",
            "target_value": "_user_id",
            "value_name": "代理付费账号",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {'terms': {'recharge_recharge_type.keyword': ['wechat', 'mf_pay']}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "agent_new_recharge_user",
            "target_value": "_user_id",
            "value_name": "代理新用户付费账号",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {'terms': {'recharge_recharge_type.keyword': ['wechat', 'mf_pay']}},
                {"term": {"_is_new_user": 1}},
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "recharge_count",
            "target_value": "_user_id",
            "value_name": "付费笔数",
            "filter": [
                {"term": {"_event_id": "recharge"}},
            ],
            "operation": "value_count",
        },
        {
            "value_id": "agency_recharge_count",
            "target_value": "_user_id",
            "value_name": "代理付费笔数",
            "filter": [
                {"term": {"_event_id": "recharge"}},
                {'terms': {'recharge_recharge_type.keyword': ['wechat', 'mf_pay']}},
            ],
            "operation": "value_count",
        },

        {
            'value_id': 'withdraw_amount',
            'target_value': 'withdraw_real_price',
            'value_name': '提现成功金额',
            'filter': [
                {'term': {'_event_id': 'withdraw'}}
            ]
        },
        {
            'value_id': 'withdraw_user',
            'target_value': '_user_id',
            'value_name': '提现成功用户数',
            'filter': [
                {'term': {'_event_id': 'withdraw'}}
            ],
            "operation": "cardinality",
        },
        {
            'value_id': 'union_withdraw_amount',
            'target_value': 'withdraw_real_price',
            'value_name': '银行卡提现成功金额',
            'filter': [
                {'term': {'_event_id': 'withdraw'}},
                {'term': {'withdraw_sub_type': '0'}}
            ],
        },
        {
            'value_id': 'alipay_withdraw_amount',
            'target_value': 'withdraw_real_price',
            'value_name': '支付宝提现成功金额',
            'filter': [
                {'term': {'_event_id': 'withdraw'}},
                {'term': {'withdraw_sub_type': '1'}}
            ],
        },

        {
            "value_id": "register_user_1_count",
            "target_value": "_user_id",
            "value_name": '昨日注册用户数',
            "time_range": {
                "_event_time": [-1, -1],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_1_count",
            "target_value": "_user_id",
            "value_name": '昨日注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-1, -1]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_2_count",
            "target_value": "_user_id",
            "value_name": '2日前注册用户数',
            "time_range": {
                "_event_time": [-2, -2],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_2_count",
            "target_value": "_user_id",
            "value_name": '2日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-2, -2]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_3_count",
            "target_value": "_user_id",
            "value_name": '3日前注册用户数',
            "time_range": {
                "_event_time": [-3, -3],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_3_count",
            "target_value": "_user_id",
            "value_name": '3日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-3, -3]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_4_count",
            "target_value": "_user_id",
            "value_name": '4日前注册用户数',
            "time_range": {
                "_event_time": [-4, -4],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_4_count",
            "target_value": "_user_id",
            "value_name": '4日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-4, -4]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_5_count",
            "target_value": "_user_id",
            "value_name": '5日前注册用户数',
            "time_range": {
                "_event_time": [-5, -5],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_5_count",
            "target_value": "_user_id",
            "value_name": '5日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-5, -5]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_6_count",
            "target_value": "_user_id",
            "value_name": '6日前注册用户数',
            "time_range": {
                "_event_time": [-6, -6],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_6_count",
            "target_value": "_user_id",
            "value_name": '6日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-6, -6]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_7_count",
            "target_value": "_user_id",
            "value_name": '7日前注册用户数',
            "time_range": {
                "_event_time": [-7, -7],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_7_count",
            "target_value": "_user_id",
            "value_name": '7日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-7, -7]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_8_count",
            "target_value": "_user_id",
            "value_name": '8日前注册用户数',
            "time_range": {
                "_event_time": [-8, -8],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_8_count",
            "target_value": "_user_id",
            "value_name": '8日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-8, -8]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_9_count",
            "target_value": "_user_id",
            "value_name": '9日前注册用户数',
            "time_range": {
                "_event_time": [-9, -9],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_9_count",
            "target_value": "_user_id",
            "value_name": '9日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-9, -9]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "register_user_10_count",
            "target_value": "_user_id",
            "value_name": '10日前注册用户数',
            "time_range": {
                "_event_time": [-10, -10],
            },
            'filter': [
                {'term': {'_event_id': 'register'}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "retain_user_10_count",
            "target_value": "_user_id",
            "value_name": '10日前注册用户今日活跃数',
            "time_range": {
                "_event_time": [0, 0],
                "_registered_at": [-10, -10]
            },
            'filter': [
                {'term': {'_event_id': 'active'}}
            ],
            "operation": "cardinality",
        },

        {
            "value_id": "ltv7_recharge",
            "target_value": "recharge_price",
            "value_name": "6天前新增用户累计充值金额",
            "time_range": {
                "_event_time": [-6, 0],
                "_registered_at": [-6, -6]
            },
            "filter": [
                {"term": {"_event_id": "recharge"}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "ltv14_recharge",
            "target_value": "recharge_price",
            "value_name": "13天前新增用户累计充值金额",
            "time_range": {
                "_event_time": [-13, 0],
                "_registered_at": [-13, -13]
            },
            "filter": [
                {"term": {"_event_id": "recharge"}}
            ]
        },
        {
            "value_id": "ltv30_recharge",
            "target_value": "recharge_price",
            "value_name": "29天前新增用户累计充值金额",
            "time_range": {
                "_event_time": [-29, 0],
                "_registered_at": [-29, -29]
            },
            "filter": [
                {"term": {"_event_id": "recharge"}}
            ]
        },
        {
            "value_id": "ltv60_recharge",
            "target_value": "recharge_price",
            "value_name": "59天前新增用户累计充值金额",
            "time_range": {
                "_event_time": [-59, 0],
                "_registered_at": [-59, -59]
            },
            "filter": [
                {"term": {"_event_id": "recharge"}}
            ]
        },
        {
            "value_id": "ltv7_user",
            "target_value": "_user_id",
            "value_name": "6天前新增用户数",
            "time_range": {
                "_event_time": [-6, -6],
            },
            "filter": [
                {"term": {"_event_id": "register"}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "ltv14_user",
            "target_value": "_user_id",
            "value_name": "13天前新增用户数",
            "time_range": {
                "_event_time": [-13, -13],
            },
            "filter": [
                {"term": {"_event_id": "register"}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "ltv30_user",
            "target_value": "_user_id",
            "value_name": "29天前新增用户数",
            "time_range": {
                "_event_time": [-29, -29],
            },
            "filter": [
                {"term": {"_event_id": "register"}}
            ],
            "operation": "cardinality",

        },
        {
            "value_id": "ltv60_user",
            "target_value": "_user_id",
            "value_name": "59天前新增用户数",
            "time_range": {
                "_event_time": [-59, -59],
            },
            "filter": [
                {"term": {"_event_id": "register"}}
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "profit",
            "target_value": ["cp_game_price", "+", "cp_lottery_price", "-", "cp_lottery_win", "-",
                             "cp_lottery_bonus", '-', "cp_game_win", '-', "cp_lottery_refund"],
            "value_name": "自有用户营收金额",
            "filter": [
                {'terms': {'_event_id': ['cp_game', 'cp_lottery']}}
            ],
        },
        {
            "value_id": "win_amount",
            "value_name": "系统输赢",
            "value_expr": "profit"
        },
        {
            "value_id": "game_active_user",
            "target_value": "_user_id",
            "value_name": "游戏活跃用户",
            "filter": [
                {'terms': {'_event_id': ['cp_game', 'cp_lottery']}}
            ],
            "operation": "cardinality",
        }
    ]
}


LOTTERY_GAME_REPORT = {
    'real_time_report': True,
    'dynamic_argument': True,
    'group_by': 'cp_lottery_sub_type',
    'target_event_id': ['cp_lottery'],
    'values': [
        {
            "value_id": "choushui_amount",
            "target_value": ["cp_lottery_price", "-", "cp_lottery_win", "-",
                             "cp_lottery_bonus", '-', "cp_lottery_refund"],
            "value_name": "抽水金额",
            "filter": [
            ],
        },
        {
            "value_id": "win_amount",
            "value_name": "系统输赢",
            "value_expr": "choushui_amount"
        },
        {
            'value_id': 'user_count',
            'target_value': '_user_id',
            'value_name': '活跃账号',
            "filter": [
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "first_recharge_user_count",
            "target_value": "_user_id",
            "value_name": "新充游戏账号",
            "filter": [
                {"terms": {"_user_id": "${first_recharge_user_pool}"}}
            ],
            "operation": "cardinality"
        },
        {
            'value_id': 'new_user_count',
            'target_value': '_user_id',
            'value_name': '新增活跃账号',
            'filter': [
                {'term': {'_is_new_user': 1}}
            ],
            'operation': 'cardinality',
        },
        {
            "value_id": "choushui_amount_avg",
            "value_name": "人均抽水金额",
            "value_expr": "choushui_amount / user_count"
        },
        {
            "value_id": "choushui_amount_ratio",
            "value_name": "抽水占比",
            "value_expr": "choushui_amount / ${total_choushui_amount}"
        }
    ]
}


CP_GAME_REPORT = {
    'real_time_report': True,
    'dynamic_argument': True,
    'group_by': 'cp_game_third_type.keyword',
    'target_event_id': ['cp_game'],
    'values': [
        {
            "value_id": "choushui_amount",
            "target_value": ["cp_game_price", '-', "cp_game_win"],
            "value_name": "抽水金额",
            "filter": [
            ],
        },
        {
            "value_id": "win_amount",
            "value_name": "系统输赢",
            "value_expr": "choushui_amount"
        },
        {
            'value_id': 'user_count',
            'target_value': '_user_id',
            'value_name': '活跃账号',
            "filter": [
            ],
            "operation": "cardinality",
        },
        {
            "value_id": "first_recharge_user_count",
            "target_value": "_user_id",
            "value_name": "新充游戏账号",
            "filter": [
                {"terms": {"_user_id": "${first_recharge_user_pool}"}}
            ],
            "operation": "cardinality"
        },
        {
            'value_id': 'new_user_count',
            'target_value': '_user_id',
            'value_name': '新增活跃账号',
            'filter': [
                {'term': {'_is_new_user': 1}}
            ],
            'operation': 'cardinality',
        },
        {
            "value_id": "choushui_amount_avg",
            "value_name": "人均抽水金额",
            "value_expr": "choushui_amount / user_count"
        },
        {
            "value_id": "choushui_amount_ratio",
            "value_name": "抽水占比",
            "value_expr": "choushui_amount / ${total_choushui_amount}"
        }
    ]
}
